package cc.lzmsh.service.impl;

import cc.lzmsh.constants.SystemConstants;
import cc.lzmsh.domain.entity.Article;
import cc.lzmsh.domain.entity.Category;
import cc.lzmsh.domain.result.Result;
import cc.lzmsh.domain.vo.ArticleDetailVo;
import cc.lzmsh.domain.vo.ArticleListVo;
import cc.lzmsh.domain.vo.HotArticleVo;
import cc.lzmsh.mapper.ArticleMapper;
import cc.lzmsh.service.ArticleService;
import cc.lzmsh.service.CategoryService;
import cc.lzmsh.utils.BeanCopyUtils;
import cc.lzmsh.utils.RedisCache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 文章表(Article)表服务实现类
 *
 * @author msh
 * @since 2024-09-13 15:07:45
 */
@Service("articleService")
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisCache redisCache;

    @Override
    public Result<List<HotArticleVo>> hotArticleList() {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        // 查询是否为正式文章
        wrapper.eq("status", SystemConstants.ARTICLE_STATUS_NORMAL);
        // 查询访问量最多
        wrapper.orderByDesc("view_count");
        // 最多显示10条
        Page<Article> page = new Page<>(1, 10);
        page = page(page, wrapper);
        List<Article> articleList = page.getRecords();

//        ArrayList<HotArticleVo> articles = new ArrayList<>();
//        for (Article article : articleList) {
//            HotArticleVo hotArticleVo = new HotArticleVo();
//            BeanUtils.copyProperties(article, hotArticleVo);
//            articles.add(hotArticleVo);
//        }

        List<HotArticleVo> articles = BeanCopyUtils.copyBeanList(articleList, HotArticleVo.class);

        return Result.success(articles);
    }

    @Override
    public List<ArticleListVo> getArticleList(Integer pageNum, Integer pageSize, Long categoryId) {
        // 查询条件
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        // 判断状态是否为正式发布
        wrapper.eq("status", SystemConstants.ARTICLE_STATUS_NORMAL);
        // 按置顶排序
        wrapper.orderByDesc("is_top");
        // 判断分类id是否为空
        if (categoryId != null && categoryId > 0) {
            // 查询条件
            wrapper.eq("category_id", categoryId);
        }
        if (pageNum != null && pageSize != null) {
            // 分页查询
            Page<Article> page = new Page<>(pageNum, pageSize);
            page(page, wrapper);
            List<Article> articleList = page.getRecords();
            articleList = articleList.stream()
                    .map(article -> article.setCategoryName(categoryService.getById(article.getCategoryId()).getName()))
                    .collect(Collectors.toList());
            return BeanCopyUtils.copyBeanList(articleList, ArticleListVo.class);
        }
        List<Article> articleList = list(wrapper);
        articleList = articleList.stream()
                .map(article -> article.setCategoryName(categoryService.getById(article.getCategoryId()).getName()))
                .collect(Collectors.toList());
        return BeanCopyUtils.copyBeanList(articleList, ArticleListVo.class);
    }

    @Override
    public ArticleDetailVo getArticleDetail(Long id) {
        Article article = getById(id);
        // 从 Redis 中获取浏览次数
        Integer viewCount = redisCache.getCacheMapValue("article:viewCount", id.toString());
        if (viewCount != null) {
            // 将 Integer 转换为 Long
            Long longViewCount = viewCount.longValue();
            article.setViewCount(longViewCount);
        }
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
        Category category = categoryService.getById(article.getCategoryId());
        articleDetailVo.setCategoryName(category.getName());
        return articleDetailVo;
    }

    @Override
    public Result updateViewCount(Long id) {
        // 更新redis中的浏览量
        redisCache.incrementCacheMapValue("article:viewCount", id.toString(), 1);
        return Result.success();
    }


}

